package com.tao.com.xiaowen.module.me;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.R;
import com.tao.com.xiaowen.adapter.MyFavoriteAdapter;
import com.tao.com.xiaowen.config.ToastUtil;
import com.tao.com.xiaowen.config.sqlite.DatabaseUtils;
import com.tao.com.xiaowen.model.Favorite;
import com.tao.com.xiaowen.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tao on 17-2-23.
 */

public class MyFavoriteActivity extends AppCompatActivity {
    private XRecyclerView recyclerView;
    private List<Favorite> favoriteList;
    private MyFavoriteAdapter myFavoriteAdapter;

    private ImageButton returnBtn;
    private TextView titleView;

    private int SIZE = 5;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);

        initView();
    }

    private void initView() {
        recyclerView = (XRecyclerView) findViewById(R.id.recyclerView);
        returnBtn = (ImageButton) findViewById(R.id.return_button);
        titleView = (TextView) findViewById(R.id.title);

        titleView.setText("我的收藏");
        returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.obtainMessage();
                Message message = new Message();
                message.what = Model.FAVORITE_REFRESH;
                handler.sendMessage(message);
            }

            @Override
            public void onLoadMore() {
//                SIZE = SIZE + 5;
//                handler.obtainMessage();
//                Message message = new Message();
//                message.what = Model.FAVORITE_LOADMORE;
//                handler.sendMessage(message);
//                recyclerView.loadMoreComplete();
            }
        });
        recyclerView.setRefreshing(true);
        favoriteList = new ArrayList<>();
        myFavoriteAdapter = new MyFavoriteAdapter(this, favoriteList);
        recyclerView.setAdapter(myFavoriteAdapter);
        myFavoriteAdapter.OnItemLongClick(new MyFavoriteAdapter.OnItemClickListener() {
            @Override
            public void OnItemLongClick(View view, int position) {
                showUpdateDialog(position);

            }
        });
    }

    private void showUpdateDialog(final int position) {
        AlertDialog.Builder builer = new AlertDialog.Builder(this);
        builer.setTitle("删除");
        builer.setMessage("确定要删除");
        builer.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Favorite favorite = favoriteList.get(position);
                myFavoriteAdapter.deleteData(position);
                DatabaseUtils.getHelper().delete(favorite);
//                DatabaseUtils.getHelper().clear(Favorite.class);

            }
        });
        builer.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builer.create();
        dialog.show();
    }

    public void SelectFavorite() {
        try {
            List<Favorite> list = DatabaseUtils.getHelper().queryAll(Favorite.class, "_id desc", SIZE);
            if (list != null) {
                for (Favorite favorite : list) {
                    favoriteList.add(favorite);
                }
            }

        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Model.FAVORITE_REFRESH:
                    favoriteList.clear();
                    SelectFavorite();
                    myFavoriteAdapter.notifyDataSetChanged();
                    recyclerView.refreshComplete();
                    break;
                case Model.FAVORITE_LOADMORE:
                    SelectFavorite();
                    myFavoriteAdapter.notifyDataSetChanged();
                    recyclerView.loadMoreComplete();
                    break;
                default:
                    break;
            }
        }
    };

}
